<?

//Data loading
$title='DAFTAR PELUNASAN PIUTANG';
if (GetParam("office","")) $perush = GetCompany (GetParam("office",""));
else $perush = 'PT. MASAJI PRAYASA CARGO';

$page   = 'PAGE';

$DEFINE_FONT=0;
$pdf=new GridPDFReport("P","mm","Letter");
$periodMonth = GetParam("periodMonth","");
$periodYear  = GetParam("periodYear","");
$pdf->AddPage();
$Result = GridList ("", "", "");
if ($Result) $pdf->GridTable($Result);
$pdf->Output();

function GridList () {
    $DBConnection = new mydb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
    $fromDate = GetParam("fromDate","");
    $toDate = GetParam("toDate","");
    list($fday,$fmonth,$fyear) = explode("/",$fromDate);
    list($tday,$tmonth,$tyear) = explode("/",$toDate);

    $SQL = "SELECT *, DATE_FORMAT(tglbkt,'%d/%m/%y') as trdate FROM ais_piutang WHERE srt=2 ";
    if (GetParam("office",""))
    $SQL .= " AND ktrasl='".GetParam("office","")."'";
        if (GetParam("fromSkada",""))
        $SQL .= " AND skada>='".GetParam("fromSkada","")."' ";
        if (GetParam("toSkada",""))
        $SQL .= " AND skada<='".GetParam("toSkada","")."' ";
        if (GetParam("fromDate","")) {
            list($fday,$fmonth,$fyear) = explode("/",GetParam("fromDate",""));
            $SQL .= " AND tglbkt>='$fyear-$fmonth-$fday' ";
        }
        if (GetParam("toDate","")) {
            list($tday,$tmonth,$tyear) = explode("/",GetParam("toDate",""));
            $SQL .= " AND tglbkt<='$tyear-$tmonth-$tday' ";
        }
        if (GetParam("curr","")) {
            $SQL .= " AND curr='".GetParam("curr","")."' ";
        }

    $SQL .= " ORDER BY debnam";
//  print $SQL."<br><br>\n\n\n\n";
    $MyResult = $DBConnection->dbc->get_results($SQL,ARRAY_A);
    return $MyResult;
}


class GridPDFReport extends PDF {

    function Header() {
        global $title,$perush,$page,$DEFINE_FONT;
        if ($DEFINE_FONT==0) {
            $this->AddFont('arialn','','arialn.php');
            $DEFINE_FONT=1;
        }
        //$this->SetLeftMargin(5);
        $this->SetFont('arialn','',10);
        $this->Cell(200,5,$perush,0,1,'C',0);
        $this->Cell(200,5,$title,0,1,'C',0);
        $this->Cell(200,5,"PERIOD: ".GetParam("fromDate","")." - ".GetParam("toDate",""),0,1,'C',0);
        $this->SetLeftMargin(5);
        $this->Cell(195,5,$page." ".$this->PageNo(),0,1,'R',0);
        $this->Line(5,30,205,30);
        $this->Ln();
        $this->Ln();
        //Header
        //Column widths

        $w=array(60,25,20,70,25);
        //$this->SetFont('arialn','',10);
        $this->Cell($w[0],7,"DEBTUR",1,0,'C',0);
        $this->Cell($w[1],7,"NO. BUKTI",1,0,'C',0);
        $this->Cell($w[2],7,"DATE",1,0,'C',0);
        $this->Cell($w[3],7,"URAIAN",1,0,'C',0);
        $this->Cell($w[4],7,"JUMLAH",1,0,'C',0);
        $this->Ln();
        $this->SetFont('arialn','',8);
    }

    function GridTable($Result) {
        //Column widths
        $w=array(60,25,20,70,25);
        //Data
        foreach($Result as $row)
        {
            if ($row["dk"]=="D") $row["debet"] = $row["jumlah"];
            else $row["credit"] = $row["jumlah"];
            $total = $total + $row["jumlah"];

            if (($lastdebnam<>$row["debnam"]) && (strlen($lastdebnam)>0)) {
                    $this->Cell($w[0]+$w[1]+$w[2]+$w[3],7,"SUB TOTAL:",1,0,'R',0);
                    $this->Cell($w[4],7,FormatNumber($subtotal,2),1,0,'R',0);
                    $this->Ln();
                    $subtotal = 0;
            }
             $subtotal = $subtotal + $row["jumlah"];

            if (($lastdebnam<>$row["debnam"]) )
                $this->Cell($w[0],6,$row["debnam"],'LR');
                        else
                $this->Cell($w[0],6,"",'LR');

            $this->Cell($w[1],6,$row["nobkt"],'LR');
            $this->Cell($w[2],6,$row["trdate"],'LR',0,'C');
            $this->Cell($w[3],6,"PAID ".$row["nobkta"],'LR');
            $this->Cell($w[4],6,FormatNumber($row["jumlah"],2),'LR',0,'R');
            $this->Ln();
            $lastdebnam = $row["debnam"];
        }
        $this->Cell($w[0]+$w[1]+$w[2]+$w[3],7,"SUB TOTAL:",1,0,'R',0);
        $this->Cell($w[4],7,FormatNumber($subtotal,2),1,0,'R',0);
        $this->Ln();

        $this->Cell($w[0]+$w[1]+$w[2]+$w[3],7,"JUMLAH: ",1,0,'R',0);
        $this->Cell($w[4],7,FormatNumber($total,2),1,0,'R',0);
        $this->Ln();

        //Closure line
        //$this->Cell(array_sum($w),0,'','T');
    }

    function Footer() {
        //Closure line
        $this->Cell(200,0,'','T');
    }

}


?>
